package com.xd100.lzll.gm.util.server;

import java.nio.charset.Charset;
import java.util.Date;

import org.apache.mina.core.buffer.IoBuffer;

public final class DBBuffer {
	private IoBuffer b = IoBuffer.allocate(256).setAutoExpand(true);
	public static Charset charset = Charset.forName("UTF-8");
	public static final int ARRAY_MAX_SIZE = 1000;

	private DBBuffer() {
	}

	public static DBBuffer warp(byte[] src) {
		DBBuffer db = new DBBuffer();
		db.b = IoBuffer.wrap(src);
		return db;
	}

	public void position(int pos) {
		b.position(pos);
	}

	public static DBBuffer allocate() {
		DBBuffer db = new DBBuffer();
		return db;
	}

	public void putInt(int in) {
		b.putInt(in);
	}

	public void putStringArray(String[] strs) {
		int len = 0;
		if (null != strs)
			len = Math.min(strs.length, ARRAY_MAX_SIZE);
		b.putInt(len);
		for (int i = 0; i < len; i++) {
			putString(strs[i]);
		}
	}

	public void putIntegerArray(Integer[] ins) {
		int len = 0;
		if (null != ins)
			len = Math.min(ins.length, ARRAY_MAX_SIZE);
		b.putInt(len);
		for (int i = 0; i < len; i++) {
			b.putInt(ins[i]);
		}
	}

	public void putLongArray(Long[] ins) {
		int len = 0;
		if (null != ins)
			len = Math.min(ins.length, ARRAY_MAX_SIZE);
		b.putInt(len);
		for (int i = 0; i < len; i++) {
			b.putLong(null == ins[i] ? 0 : ins[i]);
		}
	}

	public void putIntArray(int[] ins) {
		int len = 0;
		if (null != ins)
			len = Math.min(ins.length, ARRAY_MAX_SIZE);
		b.putInt(len);
		for (int i = 0; i < len; i++) {
			b.putInt(ins[i]);
		}
	}

	public void putlongArray(long[] ins) {
		int len = 0;
		if (null != ins)
			len = Math.min(ins.length, ARRAY_MAX_SIZE);
		b.putInt(len);
		for (int i = 0; i < len; i++) {
			b.putLong(ins[i]);
		}
	}

	public void putLong(long in) {
		b.putLong(in);
	}

	public void putString(String in) {
		if (null == in)
			in = "";
		byte[] inarray = in.getBytes(charset);
		b.putInt(inarray.length);
		b.put(inarray);
	}

	public void putDate(Date d) {
		if (null != d)
			b.putLong(d.getTime());
		else
			b.putLong(0L);
	}

	public void putBytes(byte[] bytes) {
		if (null == bytes)
			bytes = new byte[] {};
		b.putInt(bytes.length);
		b.put(bytes);
	}

	public byte[] toBytes() {
		int pos = b.position();
		byte[] ret = new byte[pos];
		System.arraycopy(b.array(), 0, ret, 0, pos);
		return ret;
	}

	public byte[] toBytesDirect() {
		b.shrink();
		return b.array();
	}

	public int getInt() {
		if (b.hasRemaining())
			return b.getInt();
		else
			return 0;
	}

	public String[] getStringArray() {
		if (b.hasRemaining()) {
			int len = Math.max(0, Math.min(b.getInt(), ARRAY_MAX_SIZE));
			String[] ret = new String[len];
			for (int i = 0; i < len; i++) {
				if (b.hasRemaining()) {
					ret[i] = getString();
				} else
					ret[i] = null;
			}
			return ret;
		} else {
			return new String[] {};
		}
	}

	public Integer[] getIntegerArray() {
		if (b.hasRemaining()) {
			int len = Math.max(0, Math.min(b.getInt(), ARRAY_MAX_SIZE));
			Integer[] ret = new Integer[len];
			for (int i = 0; i < len; i++) {
				if (b.hasRemaining()) {
					ret[i] = b.getInt();
				} else
					break;
			}
			return ret;
		} else {
			return new Integer[] {};
		}
	}

	public Long[] getLongArray() {
		if (b.remaining() >= 4) {
			int len = Math.max(0, Math.min(b.getInt(), ARRAY_MAX_SIZE));
			Long[] ret = new Long[len];
			for (int i = 0; i < len; i++) {
				if (b.hasRemaining()) {
					ret[i] = b.getLong();
				} else
					break;
			}
			return ret;
		} else {
			return new Long[] {};
		}
	}

	public int[] getIntArray() {
		if (b.hasRemaining()) {
			int len = Math.max(0, Math.min(b.getInt(), ARRAY_MAX_SIZE));
			int[] ret = new int[len];
			for (int i = 0; i < len; i++) {
				if (b.hasRemaining()) {
					ret[i] = b.getInt();
				} else
					break;
			}
			return ret;
		} else {
			return new int[] {};
		}
	}

	public long getLong() {
		if (b.hasRemaining())
			return b.getLong();
		else
			return 0;
	}

	public String getString() {
		if (b.hasRemaining()) {
			byte[] temp = new byte[b.getInt()];
			b.get(temp);
			return new String(temp, charset);
		} else
			return "";

	}

	public Date getDate() {
		if (b.hasRemaining()) {
			if (b.remaining() >= 8)
				return new Date(b.getLong());
			else {
				return null;
			}
		} else
			return null;

	}

	public byte[] getBytes() {
		if (b.hasRemaining()) {
			byte[] temp = new byte[b.getInt()];
			b.get(temp);
			return temp;
		} else
			return null;
	}

	public void putByte(byte in) {
		b.put(in);
	}

	public byte getByte() {
		if (b.hasRemaining())
			return b.get();
		else
			return 0;
	}

	public void putBoolean(boolean in) {
		b.put((byte) (in ? 1 : 0));
	}

	public boolean getBoolean() {
		if (b.hasRemaining())
			return b.get() == 1 ? true : false;
		else
			return false;
	}

	public boolean hasRemaining() {
		return b.hasRemaining();
	}

	public void free() {
		b.free();
	}
}
